package com.gopay.webtest.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.vo.BatchQueryVO;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.handler.Trans4020Handler;
import com.gopay.webtest.vo.GoodsInfo;
import com.gopay.webtest.xml.XmlDao;

@Controller
@RequestMapping("/trade")
public class TradeQueryController extends BaseController {
	@Resource(name = "xmlDao")
	private XmlDao xmlDao = null;
	@Resource(name="trans4020Handler")
	private Trans4020Handler trans4020Handler;
	
	private static Logger logger = Logger.getLogger(GoodsReturnController.class);
	private static String XML_FILE = "tradequery";
	private static String XML_FILE_4027 = "guaranteepay_4027";
	private static String XML_FILE_4028 = "guaranteepay_4028";
	private static String XML_FILE_BQ01 = "guaranteepay_BQ01";
	private static String TRADEINFO = "tradeinfo";
	
	@RequestMapping(method = RequestMethod.GET, value = "/tradeQuery")
	public ModelAndView tradeQuery() {
		ModelAndView mv = new ModelAndView();
		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(XML_FILE,
					TRADEINFO);
			mv.addObject("goodsinfo", map);
			mv.setViewName("gopay.webtest.pay.tradequery");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/tradeQuerySubmit")
	public ModelAndView tradeQuerySubmit(GoodsInfo goodsinfo) {
		ModelAndView mv = new ModelAndView(
				"gopay.webtest.pay.tradeQuerySubmit");
		/*** 对表单数据进行处理 begin */
		try {
			// 进行加签
			trans4020Handler.sign(goodsinfo.getTranCode()+"_"+goodsinfo.getVersion(), "signType",
					"signValue", goodsinfo, getRequest());
		} catch (Exception e) {
			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.addObject("goodsinfo",goodsinfo);
		return mv;
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/tradeQuery4027")
	public ModelAndView tradeQuery4027() {
		ModelAndView mv = new ModelAndView();
		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(XML_FILE_4027,
					"payapply");
			mv.addObject("goodsinfo", map);
			mv.setViewName("gopay.webtest.pay.tradequery4027");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/tradeQuerySubmit4027")
	public ModelAndView tradeQuerySubmit4027(GoodsInfo goodsinfo) {
		ModelAndView mv = new ModelAndView(
				"gopay.webtest.pay.tradeQuerySubmit4027");
		/*** 对表单数据进行处理 begin */
		try {
			// 进行加签
			trans4020Handler.sign(goodsinfo.getTranCode(), "signType",
					"signValue", goodsinfo, getRequest());
		} catch (Exception e) {
			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.addObject("goodsinfo",goodsinfo);
		return mv;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/tradeQuery4028")
	public ModelAndView tradeQuery4028() {
		ModelAndView mv = new ModelAndView();
		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(XML_FILE_4028,
					"payapply");
			mv.addObject("goodsinfo", map);
			mv.setViewName("gopay.webtest.pay.tradequery4028");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/tradeQueryBQ01")
	public ModelAndView tradeQueryBQ01() {
		ModelAndView mv = new ModelAndView();
		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(XML_FILE_BQ01,
					"payapply");
			mv.addObject("batchQueryVO", map);
			mv.setViewName("gopay.webtest.pay.tradequeryBQ01");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/tradeQuerySubmitBQ01")
	public ModelAndView tradeQuerySubmitBQ01(BatchQueryVO batchQueryVO) {
		ModelAndView mv = new ModelAndView(
				"gopay.webtest.pay.tradeQuerySubmitBQ01");
		/*** 对表单数据进行处理 begin */
		try {
			// 进行加签
			trans4020Handler.sign(batchQueryVO.getTranCode()+"_"+batchQueryVO.getVersion(), "SignType",
					"SignValue", batchQueryVO, getRequest());
		} catch (Exception e) {
			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.addObject("batchQueryVO",batchQueryVO);
		return mv;
	}

	@RequestMapping("/tradeQuerySubmit4028")
	public ModelAndView tradeQuerySubmit4028(GoodsInfo goodsinfo) {
		ModelAndView mv = new ModelAndView(
				"gopay.webtest.pay.tradeQuerySubmit4028");
		/*** 对表单数据进行处理 begin */
		try {
			// 进行加签
			trans4020Handler.sign(goodsinfo.getTranCode(), "signType",
					"signValue", goodsinfo, getRequest());
		} catch (Exception e) {
			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.addObject("goodsinfo",goodsinfo);
		return mv;
	}


}
